package it.unibo.imola.parser.visitor.entity;

import java.util.ArrayList;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InstructionSequence {

	private Logger logger = LoggerFactory.getLogger(getClass());
	private GlobalEnvironment env = null;
	private ArrayList<Instruction> instructions = null;

	public InstructionSequence() {
		env = new GlobalEnvironment();
		instructions = new ArrayList<Instruction>(0);
	}

	public void exec() throws Exception {
		env.clear();
		for (Instruction i : instructions) {
			i.exec(env,env);
		}
	}

	public void addInstruction(Instruction i) throws Exception {
		if (i == null) {
			throw new Exception("i == null");
		}
		if (!i.check(env,env)) {
			if(logger.isDebugEnabled())
				logger.debug("!i.check(env,env)");
		}
		else{
			instructions.add(i);
		}
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}
}
